package com.lagou.rpc.consumer.loadbalance;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

public abstract class AbstractLoadBalance implements LoadBalanceStrategy{

    @Override
    public String route(Map<String, String> clientPool, String serviceClassName) {
        //判断是否有可连接服务端
        if (clientPool.isEmpty()) {
            return null;
        }
        List<String> list = new CopyOnWriteArrayList<>();
        //获取这个服务下的可连接地址
        for (String key : clientPool.keySet()) {
            if(key.startsWith(serviceClassName)){
                list.add(clientPool.get(key));
            }
        }
        return doSelect(list);
    }

    protected abstract String doSelect(List<String> list);
}
